<?php

/**
 * EmsBanner form.
 *
 * @package    ems1
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class EmsBannerForm extends BaseEmsBannerForm
{
    public function configure()
    {
        unset ($this['created_at'], $this['updated_at'], $this['element_id']);

        $this->widgetSchema->setHelp('link', 'Por ejemplo: http://www.ejemplo.com');
        $this->validatorSchema['link'] = new sfValidatorUrl(array(),
                array(
                    'invalid' => 'La dirección "%value%" no tiene formato válido.',
                    'max_length'=>'La dirección excede el máximo de caracteres (%max_length% max).'
                ));
        $this->validatorSchema['link']->setOption('required', false);
        $this->validatorSchema['link']->setOption('trim', true);

        $this->widgetSchema['is_local'] = new sfWidgetFormChoice(array(
            'choices'  => Doctrine_Core::getTable('EmsBanner')->getBooleanOptions(),
            'multiple' => false,
            'expanded' => true,
        ));

        $this->widgetSchema['is_local'] = new sfWidgetFormChoice(array(
            'choices'  => Doctrine_Core::getTable('EmsBanner')->getBooleanOptions(),
            'multiple' => false,
            'expanded' => true,
        ));

        $this->validatorSchema['is_local'] = new sfValidatorChoice(array(
            'choices' => array_keys(Doctrine_Core::getTable('EmsBanner')->getBooleanOptions()),
        ));

        $context = sfContext::getInstance();

        $this->widgetSchema['section_id']->setOption(
                    'renderer_class',
                    'sfWidgetFormDoctrineJQueryAutocompleter'
                );

        $this->widgetSchema['section_id']->setOption('renderer_options',
                array(
                    'model' => 'EmsSection',
                    'url'   => $context->getController()->genUrl('element_getsections'),
                    'config' => '{ limit: 20, extraParams: { portal_id:'.$this->getOption('portal_id').'}}'
                ));

        $sizes = Doctrine::getTable('EmsBannerSize')->getCollectionOrderedById();
        $images = $this->getObject()->getImages();
        
        if($images->count()>0){
            foreach ($images as $image){
                $sizes->remove($image->getBannerSizeId());
            }
        }
        
        foreach ($sizes as $i=>$size) {
            $image = new EmsBannerImage();
            $image->setSize($size);
            $this->getObject()->getImages()->add($image);
            $images->add($image);
        }

        
        $images_forms = new sfForm();
        foreach ($images as $i=>$image){
            $image_form = new EmsBannerImageForm($image,array('portal_id' => $this->getOption('portal_id')));
            $images_forms->embedForm($i, $image_form);
        }
        
        $images_forms->validatorSchema->setPostValidator(new ImageFilesValidatorSchema(array('empty' => 'Debe cargar al menos un archivo.')));
        
        $this->embedForm('Images', $images_forms);

        $this->widgetSchema->setLabels(array(
                'link'       => 'URL',
                'section_id' => 'Sección',
                'is_local'   => 'Con link'
            ));
    }
}
